package com.wgchao.authmanager.query;

import com.wgchao.authmanager.application.vo.AgencyVo;
import com.wgchao.authmanager.infrastructure.convert.ev.AgencyInfoConvertVo;
import com.wgchao.authmanager.infrastructure.jpa.entity.AgencyInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.AgencyInfoJpaRepository;
import com.wgchao.authmanager.query.specification.AgencyCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @作者：wgchao
 * @时间：10:46 2020/5/5 0005
 * @用途：
 **/
@Service
public class AgencyInfoQuery {
    @Autowired
    private AgencyInfoJpaRepository agencyInfoJpaRepository;
    @Autowired
    private AgencyInfoConvertVo agencyInfoConvertVo;

    public List<AgencyVo> searchAgencysOfSpec(AgencyCriteria agencyCriteria) throws Exception{
        Specification<AgencyInfo> specification = new Specification<AgencyInfo>() {
            @Override
            public Predicate toPredicate(Root<AgencyInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isEmpty(agencyCriteria.getAgencyCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("agencyCode"), agencyCriteria.getAgencyCode());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(agencyCriteria.getAgencyName())){
                    Predicate predicate = criteriaBuilder.like(root.get("agencyName"), "%"+agencyCriteria.getAgencyName()+"%");
                    predicateList.add(predicate);
                }
                if(agencyCriteria.getDisabled() != null){
                    if(agencyCriteria.getDisabled()){
                        Predicate predicate = criteriaBuilder.isTrue(root.get("disabled"));
                        predicateList.add(predicate);
                    }else{
                        Predicate predicate = criteriaBuilder.isFalse(root.get("disabled"));
                        predicateList.add(predicate);
                    }
                }

                Predicate predicate = criteriaBuilder.isFalse(root.get("del"));
                predicateList.add(predicate);

                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };

        List<AgencyInfo> agencyInfos = agencyInfoJpaRepository.findAll(specification);
        if(agencyInfos==null || agencyInfos.isEmpty()){
            return new ArrayList<>();
        }

        List<AgencyVo> agencyVos = agencyInfos.stream().map(agencyInfo -> {
            return agencyInfoConvertVo.toModel(agencyInfo);
        }).collect(Collectors.toList());

        return agencyVos;
    }
}
